import React, { lazy } from 'react'
import { createBrowserRouter, Navigate } from 'react-router-dom'
import type { RouteObject } from 'react-router-dom'

import {
  CalendarOutlined,
  WarningOutlined,
  FileAddOutlined,
  ScheduleOutlined,
} from '@ant-design/icons'

const BeforeEach = lazy(() => import('@/components/BeforeEach'))
const Home = lazy(() => import('@/views/Home'))
const Login = lazy(() => import('@/views/Login'))
const Sign = lazy(() => import('@/views/Sign'))
const Exception = lazy(() => import('@/views/Exception'))
const Apply = lazy(() => import('@/views/Apply'))
const Check = lazy(() => import('@/views/Check'))
const NotAuth = lazy(() => import('@/views/NotAuth'))
const NotFound = lazy(() => import('@/views/NotFound'))
const NotServer = lazy(() => import('@/views/NotServer'))

declare module 'react-router-dom' {
  interface IndexRouteObject {
    name?: string
    meta?: {
      menu?: boolean
      title?: string
      icon?: React.ReactNode
      auth?: boolean
    }
  }
  interface NonIndexRouteObject {
    name?: string
    meta?: {
      menu?: boolean
      title?: string
      icon?: React.ReactNode
      auth?: boolean
    }
  }
}

export const routes: RouteObject[] = [
  {
    path: '/',
    name: '',
    element: React.createElement(Navigate, { to: '/sign' }),
  },
  {
    path: '/',
    name: 'home',
    element: React.createElement(BeforeEach, null, React.createElement(Home)),
    meta: {
      menu: true,
      title: '考勤系统',
      auth: true,
    },
    children: [
      {
        path: 'sign',
        name: 'sign',
        element: React.createElement(Sign),
        meta: {
          menu: true,
          title: '在线打卡签到',
          icon: React.createElement(CalendarOutlined),
          auth: true,
        },
      },
      {
        path: 'exception',
        name: 'exception',
        element: React.createElement(Exception),
        meta: {
          menu: true,
          title: '异常考勤查询',
          icon: React.createElement(WarningOutlined),
          auth: true,
        },
      },
      {
        path: 'apply',
        name: 'apply',
        element: React.createElement(Apply),
        meta: {
          menu: true,
          title: '添加考勤审批',
          icon: React.createElement(FileAddOutlined),
          auth: true,
        },
      },
      {
        path: 'check',
        name: 'check',
        element: React.createElement(Check),
        meta: {
          menu: true,
          title: '我的考勤审批',
          icon: React.createElement(ScheduleOutlined),
          auth: true,
        },
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    element: React.createElement(Login),
  },
  {
    path: '/403',
    element: React.createElement(NotAuth),
  },
  {
    path: '/404',
    element: React.createElement(NotFound),
  },
  {
    path: '/500',
    element: React.createElement(NotServer),
  },
  {
    path: '*',
    element: React.createElement(Navigate, { to: '/404' }),
  },
]

const router = createBrowserRouter(routes)

export default router
